package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IGregorianCalendarDAO;
import org.jsola.hr.dto.GregorianCalendarAddDTO;
import org.jsola.hr.dto.GregorianCalendarUpdateDTO;
import org.jsola.hr.entity.GregorianCalendarDO;
import org.jsola.hr.query.GregorianCalendarQuery;
import org.jsola.hr.service.IGregorianCalendarService;
import org.jsola.hr.vo.GregorianCalendarListVO;
import org.jsola.hr.vo.GregorianCalendarVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrGregorianCalendarServiceImpl")
public class GregorianCalendarServiceImpl implements IGregorianCalendarService {

    @Autowired
    private IGregorianCalendarDAO gregorianCalendarDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public GregorianCalendarVO save(GregorianCalendarAddDTO gregorianCalendarAddDTO, TokenUser tokenUser) {
        // 转DO
        GregorianCalendarDO gregorianCalendarDO = gregorianCalendarAddDTO.to(GregorianCalendarDO.class);
        // 保存
        gregorianCalendarDO = save(gregorianCalendarDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return gregorianCalendarDO.to(GregorianCalendarVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(GregorianCalendarUpdateDTO gregorianCalendarUpdateDTO, TokenUser tokenUser) {
        //转DO
        GregorianCalendarDO gregorianCalendarDO = gregorianCalendarUpdateDTO.to(GregorianCalendarDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(gregorianCalendarDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... gregorianCalendarIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) gregorianCalendarIds);
    }


    @Override
    public GregorianCalendarVO selectById(String gregorianCalendarId, String siteId) {
        GregorianCalendarDO gregorianCalendarDO = selectDOById(gregorianCalendarId, siteId);
        if (gregorianCalendarDO == null) {
            return null;
        }
        return gregorianCalendarDO.to(GregorianCalendarVO.class);
    }

    @Override
    public List<GregorianCalendarListVO> select(GregorianCalendarQuery gregorianCalendarQuery, String siteId) {
        List<GregorianCalendarDO> gregorianCalendarDOList = selectDO(gregorianCalendarQuery, siteId);
        if(CollectionUtils.isEmpty(gregorianCalendarDOList)) {
            return gregorianCalendarDOList == null ? null : new ArrayList<>();
        }
        return gregorianCalendarDOList.stream()
                .map(gregorianCalendarDO -> gregorianCalendarDO.to(GregorianCalendarListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(GregorianCalendarQuery gregorianCalendarQuery, String siteId) {
        Example example = buildExample(gregorianCalendarQuery,siteId);
        return gregorianCalendarDAO.selectCountByExample(example);
    }

    @Override
    public Page<GregorianCalendarListVO> selectPage(GregorianCalendarQuery gregorianCalendarQuery, String siteId) {
        Example example = buildExample(gregorianCalendarQuery,siteId);
        Page<GregorianCalendarDO> page = gregorianCalendarDAO.selectPageByExample(example,
                gregorianCalendarQuery.getPageNo(),
                gregorianCalendarQuery.getPageSize());

        return page.to(GregorianCalendarListVO.class);
    }

    @Override
    public GregorianCalendarDO selectDOById(String gregorianCalendarId, String siteId) {
        return listById(gregorianCalendarId, siteId);
    }

    @Override
    public List<GregorianCalendarDO> selectDO(GregorianCalendarQuery gregorianCalendarQuery, String siteId) {
        Example example = buildExample(gregorianCalendarQuery,siteId);
        return gregorianCalendarDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param gregorianCalendarQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(GregorianCalendarQuery gregorianCalendarQuery, String siteId) {
        Example example = new Example(GregorianCalendarDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (gregorianCalendarQuery.getYearly() != null) {
            example.and().andEqualTo("yearly", gregorianCalendarQuery.getYearly());
        }
        if (gregorianCalendarQuery.getMonthly() != null) {
            example.and().andEqualTo("monthly", gregorianCalendarQuery.getMonthly());
        }
        if (gregorianCalendarQuery.getDaily() != null) {
            example.and().andEqualTo("daily", gregorianCalendarQuery.getDaily());
        }
        if (gregorianCalendarQuery.getDateStr() != null) {
            example.and().andLike("dateStr", "%" + gregorianCalendarQuery.getDateStr() + "%");
        }
        if (gregorianCalendarQuery.getDateType() != null) {
            example.and().andEqualTo("dateType", gregorianCalendarQuery.getDateType());
        }
        if (gregorianCalendarQuery.getDateTypeNotEq() != null) {
            example.and().andNotEqualTo("dateType", gregorianCalendarQuery.getDateTypeNotEq());
        }
        if (gregorianCalendarQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", gregorianCalendarQuery.getCompanyId());
        }
        if (gregorianCalendarQuery.getGmtCreateStart() != null &&
                gregorianCalendarQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", gregorianCalendarQuery.getGmtCreateStart(),
                    gregorianCalendarQuery.getGmtCreateEnd());
        }
        if (gregorianCalendarQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + gregorianCalendarQuery.getCreateUserId() + "%");
        }
        if (gregorianCalendarQuery.getGmtModifiedStart() != null &&
                gregorianCalendarQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", gregorianCalendarQuery.getGmtModifiedStart(),
                    gregorianCalendarQuery.getGmtModifiedEnd());
        }
        if (gregorianCalendarQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + gregorianCalendarQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example,gregorianCalendarQuery.getOrders());
        return example;
    }
}




